package demo7;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.Socket;

@Component
@PropertySource("classpath:config.properties")
public class Client {
    private String url;
    private int port;
    private Socket socket;

    public String getUrl() {
        return url;
    }
    @Value("${server.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    public int getPort() {
        return port;
    }
    @Value("${server.port}")
    public void setPort(int port) {
        this.port = port;
    }
    public void init(){
        //与服务端建立链接
        try {
            socket = new Socket(url,port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void destroy(){
        if(socket!=null){
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}









